package com.eascs.workflow.email.service.impl;

import com.eascs.notify.sms.SmsNotifyRequest;
import com.eascs.notify.sms.SmsNotifyResponse;
import com.eascs.workflow.common.entity.sys.WfMessage;
import com.eascs.workflow.email.dao.SysSendEmailOrMessageDao;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.DigestUtils;
import com.eascs.common.util.UuidUtil;
import com.eascs.notify.constant.ModelResult;
import com.eascs.notify.sms.SmsNotifier;

@Service("smsServices")
public class SmsNotifierImpl implements SmsNotifier {
	private static Logger log = LoggerFactory.getLogger(SmsNotifierImpl.class);
	private final static String SYS_FN = "[Workflow][SmsNotifier]";//系统+功能模块

	@Autowired
	private SysSendEmailOrMessageDao dao;

	@Override
	public ModelResult<SmsNotifyResponse>  notify(SmsNotifyRequest smsNotifyRequest) {
		
		ModelResult<SmsNotifyResponse> model = new ModelResult<SmsNotifyResponse>();
 		SmsNotifyResponse smsNotifyResponse = new SmsNotifyResponse();
		if (StringUtils.isBlank(smsNotifyRequest.getSystemCode())) {
			log.error(SYS_FN + "系统编码为空！");
			return model.withError(SmsErrorDic.CHECKERROR, "系统编码为空！");
		}
		if (StringUtils.isBlank(smsNotifyRequest.getRecipientNumber())) {
			log.info(SYS_FN + "收件人手机号为空！");
			return model.withError(SmsErrorDic.CHECKERROR, SYS_FN + "收件人手机号为空！");
		}
		if (StringUtils.isBlank(smsNotifyRequest.getContent())) {
			log.info(SYS_FN + "短信内容为空！");
			return model.withError(SmsErrorDic.CHECKERROR, SYS_FN + "短信内容为空！");
		}
		if (StringUtils.isBlank(smsNotifyRequest.getReceiveName())) {
			log.info(SYS_FN + "收件人姓名为空！");
			return model.withError(SmsErrorDic.CHECKERROR, SYS_FN + "收件人姓名为空！");
		}
		WfMessage wfMessage = new WfMessage();
		wfMessage.setRequestId(smsNotifyRequest.getRequestId());
		wfMessage.setBizCode(smsNotifyRequest.getBizCode());
		wfMessage.setSystemCode(smsNotifyRequest.getSystemCode());
		wfMessage.setContent(smsNotifyRequest.getContent());
		wfMessage.setReceiveName(smsNotifyRequest.getReceiveName());
		wfMessage.setReceiveTelphone(smsNotifyRequest.getRecipientNumber());
		wfMessage.setSendName(smsNotifyRequest.getSendName());
		wfMessage.setSendType(smsNotifyRequest.getSmsSendType());
		wfMessage.setRequestDateTime(new Date());
		wfMessage.setScheduleSendDateTime(smsNotifyRequest.getScheduleSendDateTime());
		wfMessage.setCreateTime(new Date());
		if("TEST".equals(smsNotifyRequest.getSmsSendType())){
			wfMessage.setStatus(1);
		}else{
			wfMessage.setStatus(0);
		}

		int i=dao.insertNewMessage(wfMessage);
		if(i==0){
			log.error("数据库插入异常...", "");
			return model.withError(SmsErrorDic.SQLERROR, "数据库插入异常");
		}else{
			smsNotifyResponse.setResponseDateTime(smsNotifyRequest.getRequestDateTime());
			smsNotifyResponse.setResponseId(smsNotifyRequest.getRequestId());
			return model.withModel(smsNotifyResponse);
		}
	}
}
